package com.example.projectdemo.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.projectdemo.R;
import com.example.projectdemo.config.DBOpenHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChafingActivity extends AppCompatActivity {
    private List<Map<String, Object>> lists;
    private SimpleAdapter adapter;
    private DBOpenHelper mDBOpenHelper;

    private String[] theme = {"香酥火锅 160yuan", "香辣火锅 128yuan", "双拼火锅 190yuan"};
    private int[] imageViews = {R.drawable.img_5, R.drawable.img_6, R.drawable.img_7};

    private String number = "";
    private int moneyAll = 0;
    private TextView msgtxv;
    private ListView lv;
    private Map<String, Object> map;
    private String[] s;

    ArrayList<String> selected = new ArrayList<>();//存储已经选取的食物

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chafing);
        mDBOpenHelper = new DBOpenHelper(this);

        lv = (ListView) findViewById(R.id.lv2);
        //lv.setOnItemClickListener(this);
        findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] str = number.split(" ");
                for (String s : str) {
                    int money = Integer.parseInt(s);
                    moneyAll += money;
                }
                String str2 = "总价为： " + moneyAll;
                msgtxv.setText(str2);
                String s = selected.toString();
                String num = moneyAll + "";
                mDBOpenHelper.add4(s, num);
                Intent intent = new Intent(ChafingActivity.this, OrdersActivity.class);
                startActivity(intent);
                finish();
            }
        });
        //需要把图片和文字(一个单元中的东西)用Map对应起来，必须这样做，这是下面要用到的适配器的一个参数
        lists = new ArrayList<>();
        for (int i = 0; i < theme.length; i++) {
            map = new HashMap<>();
            map.put("image", imageViews[i]);
            map.put("theme", theme[i]);
            lists.add(map);
        }
        System.out.println(lists.toString());
        //适配器指定应用自己定义的xml格式
        adapter = new SimpleAdapter(ChafingActivity.this, lists, R.layout.form2, new String[]{"image", "theme"}, new int[]{R.id.image3, R.id.text3});
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //无法获取信息
                //TextView txv = (TextView) view; //将被单击的ｖｉｅｗ转化成TextView对象
                System.out.println(lists.get(i));
                String[] str = lists.get(i).toString().split(", ");//获取对象的文字
                String str2 = str[1];
                String[] str3 = str2.split(" ");
                String[] str4 = str3[0].split("=");
                String item = str4[1];
                String[] yuans = str3[1].split("yuan");
                number = number + yuans[0] + " ";
                System.out.println(number);
                //String item =txv.getText().toString();
                System.out.println(item);
                if (selected.contains(item))
                    selected.remove(item); //如果包含了就将它删除
                else
                    selected.add(item);//否则就添加

                String msg;
                if (selected.size() > 0) {
                    msg = "你点了：";
                    for (String str0 : selected)
                        msg += " " + str0; //每一个选项名称前面都加上
                } else
                    msg = "请点餐";

                msgtxv = (TextView) findViewById(R.id.testview_id2);
                msgtxv.setText(msg);
            }
        });
    }
}
